package com.kunchi.platform.controller;

import com.alibaba.fastjson.JSONObject;
import com.kunchi.platform.entity.gateway.CompanyGateway;
import com.kunchi.platform.model.gateway.Gateway;
import com.kunchi.platform.service.GatewayService;
import com.mysql.jdbc.StringUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Objects;
import java.util.stream.Stream;

/**
 * Created by denniszhou on 2017/7/23.
 */
@Controller
@RequestMapping(value = "/gateway")
public class GatewayController {

  @Autowired
  GatewayService gatewayService;

  @RequestMapping(value = "/all", method = RequestMethod.GET)
  public ArrayList<Object> getGatewayList(
      @RequestParam(name = "company_id") Long companyId,
      @RequestParam(name = "device_id", required = false) Long deviceId,
      @RequestParam(name = "device_name", required = false) String deviceName,
      @RequestParam(name = "serial_number", required = false) String serialNumber)
      throws IOException {

    Stream<Gateway> stream = gatewayService.getGateWayList().stream();
    if (!Objects.isNull(companyId)) {
      ArrayList<Object>
          serialNumbers =
          gatewayService.findByCompanyId(companyId).stream()
              .map(s -> s.getSerialNumber())
              .collect(ArrayList::new, ArrayList::add, ArrayList::add);
      stream = stream.filter(m -> serialNumbers.contains(m.getSerialNumber()));
    }

    if (!Objects.isNull(deviceId)) {
      stream =
          stream.filter(s -> s.getDeviceList().stream().anyMatch(m -> m.getDeviceId() == deviceId));
    }

    if (!StringUtils.isNullOrEmpty(deviceName)) {
      stream =
          stream.filter(
              s -> s.getDeviceList().stream().anyMatch(m -> m.getDeviceName().equals(deviceName)));
    }

    if (!StringUtils.isNullOrEmpty(serialNumber)) {
      stream =
          stream.filter(s -> s.getDeviceList().stream()
              .anyMatch(m -> m.getSerialNumber().equals(serialNumber)));
    }
    return stream.collect(ArrayList::new, ArrayList::add, ArrayList::add);
  }


  @RequestMapping(value = "/add", method = RequestMethod.POST)
  public ArrayList<Object> addGateway(@RequestBody JSONObject jsonObject) throws IOException {
    CompanyGateway
        companyGateway =
        JSONObject.parseObject(jsonObject.toJSONString(), CompanyGateway.class);
    gatewayService.add(companyGateway);
    return getGatewayList(companyGateway.getCompanyId(), null, null, null);
  }

  @RequestMapping(value = "/delete", method = RequestMethod.POST)
  public ArrayList<Object> deleteGateway(@RequestBody JSONObject jsonObject) throws IOException {
    CompanyGateway
        companyGateway =
        JSONObject.parseObject(jsonObject.toJSONString(), CompanyGateway.class);
    gatewayService.delete(companyGateway);
    return getGatewayList(companyGateway.getCompanyId(), null, null, null);
  }
}
